/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import java.util.List;
import model.Torrent;
import xmlreader.TorrentReader;
import xmlwriter.TorrentWriter;

/**
 *
 * @author Oscar
 */
public class TorrentDAO {

    private static TorrentDAO instance = new TorrentDAO();
    public List<Torrent> Torrents;


    private TorrentDAO() {

        load();
    }

    public static TorrentDAO getInstance()
    {
//        if (instance == null)
//            instance = new TorrentDAO();

        return instance;
    }

    /**
     * Devuelve la lista con todos los servidores
     * @return List de ServerInfo
     */
    public List<Torrent> getAll() {

        return this.Torrents;
    }


    /**
     * Verifica que un servidor este registrado
     *
     * @param nickname Nombre de usuario
     * @param password Clave de un Usuario
     * @return
     */
     public Torrent exist(String user, String name) {

        for (Torrent torrent : this.Torrents) {

            if (torrent.getName().equalsIgnoreCase(name)
                    && torrent.getUser().equalsIgnoreCase(user)) {

                return torrent;
            }

        }

        Torrent a = new Torrent(user,name,"0");

        this.Torrents.add(a);

        return a;
    }


    public void addBlock(String user, String name) {

        for (int i = 0; i < this.Torrents.size(); i++) {

            if (this.Torrents.get(i).getName().equalsIgnoreCase(name)
                    && this.Torrents.get(i).getUser().equalsIgnoreCase(user)) {

                this.Torrents.get(i).setBlock(String.valueOf(
                        Integer.valueOf(this.Torrents.get(i).getBlock()) + 1));
            }
        }
    }

    public void remove(String user, String name) {

        int indexRemove = -1;

        for (int i = 0; i < this.Torrents.size(); i++) {

            if (this.Torrents.get(i).getName().equalsIgnoreCase(name)
                    && this.Torrents.get(i).getUser().equalsIgnoreCase(user)) {

                indexRemove = i;
            }
        }

        if (indexRemove > -1)
            this.Torrents.remove(indexRemove);
    }

    /**
     * Crea un usuario nuevo
     *
     * @param nickname Nombre de usuario
     * @param password Clave de un Usuario
     */
    public void createTorrent(String user, String name, String block) {

        this.Torrents.add(new Torrent(user, name, block));
    }

    

    /**
     * Guarda los cambios hechos
     */
    public void commit() {

        //writing xml file

        TorrentWriter writer = new TorrentWriter();

        writer.setFile("torrents.xml");

        try{

            writer.saveConfig(this.Torrents);
        }
        catch(Exception e) {

        }
    }

    /**
     * Carga los Datos para tenerlos disponibles
     */
    public void load() {

        //reading xml file
        TorrentReader reader = new TorrentReader();

        this.Torrents = reader.readConfig("torrents.xml");
    }

}
